package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @description:
 * @author: dalao
 * @date: 2020/4/9-20:52
 * @Version: 1.0.0
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Override
    public void setNumberByDate(OrderSetting orderSetting) {
//        进行判断如果该日期没有预约 , 直接添加数据 , 如果已经预约,更新数据
//        首先根据日期查询count
        Integer count  = orderSettingMapper.queryCountByOrderDate(orderSetting);
//        进行判断 , 如果为0直接插入数据
        if(count<=0){
            orderSettingMapper.add(orderSetting);
        }else {
            orderSettingMapper.edit(orderSetting);
        }

    }

    @Override
    public void add(List<OrderSetting> orderSettingList) {
//        判断集合是否有数据
        if(orderSettingList!=null&&orderSettingList.size()>0){
            for (OrderSetting orderSetting : orderSettingList) {
                setNumberByDate(orderSetting);
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        //每月第一天
        String beginDate = date+"-1";
        //每月最后一天
        String endDate = date+"-31";
//        查询当月区间的数据
        List<OrderSetting> orderSettingList = orderSettingMapper.getOrderSettingByMonth(beginDate,endDate);
//        获取月份

//        创建集合存储数据
        List<Map> leftobj = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            Map map = new LinkedHashMap();
            map.put("date",orderSetting.getOrderDate().getDate());
            map.put("mouth",orderSetting.getOrderDate().getMonth());
            System.out.println(orderSetting.getOrderDate().getMonth());
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            leftobj.add(map);
        }
        return leftobj;
    }
}
